/**
 * Copyright &copy; 2012-2016 <a href="https://github.com/thinkgem/jeesite">JeeSite</a> All rights reserved.
 */
package com.thinkgem.jeesite.modules.hk.media.service;

import java.util.List;

import com.thinkgem.jeesite.modules.sys.entity.Area;
import com.thinkgem.jeesite.modules.sys.service.AreaService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import com.thinkgem.jeesite.common.persistence.Page;
import com.thinkgem.jeesite.common.service.CrudService;
import com.thinkgem.jeesite.common.utils.StringUtils;
import com.thinkgem.jeesite.modules.hk.media.entity.HkSystemFreight;
import com.thinkgem.jeesite.modules.hk.media.dao.HkSystemFreightDao;
import com.thinkgem.jeesite.modules.hk.media.entity.HkSystemAreaFreight;
import com.thinkgem.jeesite.modules.hk.media.dao.HkSystemAreaFreightDao;

/**
 * 系统运费模板Service
 * @author 罗天文
 * @version 2018-06-20
 */
@Service
@Transactional(readOnly = true)
public class HkSystemFreightService extends CrudService<HkSystemFreightDao, HkSystemFreight> {

	@Autowired
	private HkSystemAreaFreightDao hkSystemAreaFreightDao;
	@Autowired
	private AreaService areaService;
	public HkSystemFreight get(String id) {
		HkSystemFreight hkSystemFreight = super.get(id);
		StringBuffer sb=new StringBuffer();
		Area area;
		if(StringUtils.isNotBlank(hkSystemFreight.getProvinceId()) ){
                  String ps[]=hkSystemFreight.getProvinceId().split(",");
			for (String p:ps
				 ) {
				area=areaService.get(p);
				if(null!=area){
					sb.append(area.getName()).append(",");
				}
			}
			hkSystemFreight.setProvinceName(sb.toString());
		}
		List<HkSystemAreaFreight>lists= hkSystemAreaFreightDao.findList(new HkSystemAreaFreight(hkSystemFreight));
		for (HkSystemAreaFreight h:lists
			 ) {
			sb.setLength(0);
			if(StringUtils.isNotBlank(h.getProvinceId()) ){
				String ps[]=h.getProvinceId().split(",");
				for (String p:ps
						) {
					area=areaService.get(p);
					if(null!=area){
						sb.append(area.getName()).append(",");
					}
				}
				h.setProvinceName(sb.toString());
			}
		}
		hkSystemFreight.setHkSystemAreaFreightList(lists);
		return hkSystemFreight;
	}
	
	public List<HkSystemFreight> findList(HkSystemFreight hkSystemFreight) {
		return super.findList(hkSystemFreight);
	}
	
	public Page<HkSystemFreight> findPage(Page<HkSystemFreight> page, HkSystemFreight hkSystemFreight) {
		return super.findPage(page, hkSystemFreight);
	}
	
	@Transactional(readOnly = false)
	public void save(HkSystemFreight hkSystemFreight) {
		super.save(hkSystemFreight);
		for (HkSystemAreaFreight hkSystemAreaFreight : hkSystemFreight.getHkSystemAreaFreightList()){
			if (hkSystemAreaFreight.getId() == null){
				continue;
			}
			if (HkSystemAreaFreight.DEL_FLAG_NORMAL.equals(hkSystemAreaFreight.getDelFlag())){
				if (StringUtils.isBlank(hkSystemAreaFreight.getId())){
					hkSystemAreaFreight.setFreight(hkSystemFreight);
					hkSystemAreaFreight.preInsert();
					hkSystemAreaFreightDao.insert(hkSystemAreaFreight);
				}else{
					hkSystemAreaFreight.preUpdate();
					hkSystemAreaFreightDao.update(hkSystemAreaFreight);
				}
			}else{
				hkSystemAreaFreightDao.delete(hkSystemAreaFreight);
			}
		}
	}
	
	@Transactional(readOnly = false)
	public void delete(HkSystemFreight hkSystemFreight) {
		super.delete(hkSystemFreight);
		hkSystemAreaFreightDao.delete(new HkSystemAreaFreight(hkSystemFreight));
	}
	
}